﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;

namespace DAL
{
    public class CallMonitorService : UpdateData
    {
        CRMEntities db;

        public List<IBDetailEntity> LoadIBDetailByAll()
        {
            db = new CRMEntities();
            var query = (from IB in db.InBoundDetails
                         select new IBDetailEntity
                         {
                             DateOfComplain = IB.DateOfComplain,
                             SourceReciving = IB.SourceReciving,
                             Customer_ID = IB.Customer_ID,
                             NSX = IB.NXS,
                             HSD = IB.HSD,
                             BrandNameID = IB.BrandName_ID,
                             ReasonComplain = IB.ReasonComplain,
                             Status = IB.Status,
                             CRMResponse = IB.CRMResponse,
                             SupplyResponse = IB.SupplyResponse,
                             FactoryResponse = IB.FactoryResponse,
                             ProductExchange = IB.ProductExchange,
                             GiftProduct = IB.GiftProduct,
                             DatetoFeedBack = IB.DatetoFeedBack
                         }).ToList();

            return query;
        }

        public ArrayList LoadIBDetailByCustomerID(long _customerID)
        {
            db = new CRMEntities();
            ArrayList list = new ArrayList();
            var query = (from IB in db.InBoundDetails
                            join cus in db.Customers on IB.Customer_ID equals cus.Customer_ID
                         where IB.Customer_ID == _customerID
                         select new IBDetailEntity
                         {
                             DateOfComplain = IB.DateOfComplain,
                             SourceReciving = IB.SourceReciving,
                             Customer_ID = IB.Customer_ID,
                             NSX = IB.NXS,
                             HSD = IB.HSD,
                             BrandNameID = IB.BrandName_ID,
                             ReasonComplain = IB.ReasonComplain,
                             Status = IB.Status,
                             CRMResponse = IB.CRMResponse,
                             SupplyResponse = IB.SupplyResponse,
                             FactoryResponse = IB.FactoryResponse,
                             ProductExchange = IB.ProductExchange,
                             GiftProduct = IB.GiftProduct,
                             DatetoFeedBack = IB.DatetoFeedBack,
                             CellPhoneNumber = cus.CellPhoneNumber,
                             HomePhoneNumber = cus.HomePhoneNumber,
                             OfficePhoneNumber = cus.OfficePhoneNumber,
                         }).ToList();

            list.Add(query);

            if (query != null && query.Count > 0)
            {
                var cellphone = query[0].CellPhoneNumber;
                var homephone = query[0].HomePhoneNumber;
                var officephone = query[0].OfficePhoneNumber;
                var tmp = (from re in db.Records
                           where re.SourceDestination == cellphone ||
                           re.SourceDestination == homephone ||
                           re.SourceDestination == officephone
                           && re.Type == "IN"
                           select new RecordEntity
                           {
                               RecordID = re.ID,
                               RecordLink = re.RecordLink,
                               DateReceived = re.DateReceived,
                               Duration = re.Duration,
                           }).ToList();

                list.Add(tmp);
            }

            return list;
        }

        public void AddIBDetail(IBDetailEntity _ib)
        {
            CRMEntities db = new CRMEntities();
            try
            {
                InBoundDetail ib = new InBoundDetail();

                ib.DateOfComplain = _ib.DateOfComplain;
                ib.SourceReciving = _ib.SourceReciving;
                ib.Customer_ID = _ib.Customer_ID;
                ib.NXS = _ib.NSX;
                ib.HSD = _ib.HSD;
                ib.BrandName_ID = _ib.BrandNameID;
                ib.ReasonComplain = _ib.ReasonComplain;
                ib.Status = _ib.Status;
                ib.CRMResponse = _ib.CRMResponse;
                ib.SupplyResponse = _ib.SupplyResponse;
                ib.FactoryResponse = _ib.FactoryResponse;
                ib.ProductExchange = _ib.ProductExchange;
                ib.GiftProduct = _ib.GiftProduct;
                ib.DatetoFeedBack = _ib.DatetoFeedBack;
                db.InBoundDetails.Add(ib);
                db.SaveChanges();
            }
            catch
            {
                throw;
            }
            finally
            {
                db.Dispose();
            }            
        }

        public void UpdateIBDetail(IBDetailEntity _ib)
        {
            CRMEntities db = new CRMEntities();
            try
            {
                var query = (from ib in db.InBoundDetails
                             where ib.ID == _ib.ID
                             select ib).SingleOrDefault();
                if (query != null)
                {
                    query.DateOfComplain = _ib.DateOfComplain;
                    query.SourceReciving = _ib.SourceReciving;
                    query.Customer_ID = _ib.Customer_ID;
                    query.NXS = _ib.NSX;
                    query.HSD = _ib.HSD;
                    query.BrandName_ID = _ib.BrandNameID;
                    query.ReasonComplain = _ib.ReasonComplain;
                    query.Status = _ib.Status;
                    query.CRMResponse = _ib.CRMResponse;
                    query.SupplyResponse = _ib.SupplyResponse;
                    query.FactoryResponse = _ib.FactoryResponse;
                    query.ProductExchange = _ib.ProductExchange;
                    query.GiftProduct = _ib.GiftProduct;
                    query.DatetoFeedBack = _ib.DatetoFeedBack;
                }
                db.SaveChanges();
            }
            catch
            {
                throw;
            }
            finally
            {
                db.Dispose();
            }
            
        }

        public List<RecordEntity> getHistoryCall(long CustomerID)
        {
            db = new CRMEntities();
            var Cus = (from outbound in db.OutBoundDetails
                       join cu in db.Customers on outbound.Customer_ID equals cu.Customer_ID
                       where outbound.Customer_ID == CustomerID
                       select new
                       {
                           CellPhoneNumber = cu.CellPhoneNumber,
                           HomePhoneNumber = cu.OfficePhoneNumber,
                           OfficePhoneNumber = cu.OfficePhoneNumber,
                           CallStatus_ID = outbound.CallStatus_ID,
                       }).ToList();

            List<RecordEntity> query = new List<RecordEntity>();

            foreach (var item in Cus)
            {
                var tmp = (from re in db.Records
                            join us in db.Users on re.User_ID equals us.User_ID
                            where re.ReceivedDestination == item.CellPhoneNumber ||
                            re.ReceivedDestination == item.HomePhoneNumber ||
                            re.ReceivedDestination == item.OfficePhoneNumber
                            && re.Type == "OUT"
                            select new DTO.RecordEntity
                            {
                                RecordID = re.ID,
                                RecordLink = re.RecordLink,
                                DateReceived = re.DateReceived,
                                Duration = re.Duration,
                                User_ID = re.User_ID,
                                UserName = us.LastName + " " + us.FirstName,
                                CallStatus_ID = item.CallStatus_ID,
                            }).ToList();
                query.AddRange(tmp);
            }
            
            return query;
        }
         
 
    }
}
